<template>
  <div class="login-page">
    <div class="login-body">
      <div class="left">
        <img src="../assets/images/login_pic.png">
      </div>
      <div class="page-account-container">
        <div class="form-body">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <div class="sys-name">{{ $t('common.systemName') }}</div>
            <hr class="divider" />
            <div>
              <div v-if="showMultipleCorporate">
                <el-form-item prop="tenant" v-if="showMultipleCorporate">
                  <el-select
                    filterable
                    v-model="loginForm.tenantCode"
                    :placeholder="$t('common.selectTenant')"
                    clearable
                    style="width:100%"
                  >
                    <el-option
                      key=""
                      :label="$t('common.selectTenant')"
                      value=""
                    />
                    <el-option
                      v-for="item in tenantInfoList"
                      :key="item.tenantCode"
                      :label="item.tenantName"
                      :value="item.tenantCode"
                    />
                  </el-select>
                </el-form-item>
              </div>
              <div>
                <el-form-item prop="userName">
                  <el-input v-model="loginForm.userName" type="text" auto-complete="off" :placeholder="$t('common.username')">
                    <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                  </el-input>
                </el-form-item>
              </div>
              <div>
                <el-form-item prop="passwd">
                  <el-input
                    v-model="loginForm.inputPwd"
                    type="password"
                    auto-complete="off"
                    :placeholder="$t('common.password')"
                    @keyup.enter.native="handleLogin"
                  >
                    <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                  </el-input>
                </el-form-item>
              </div>
              <div>
                <el-form-item prop="verifyCode" v-if="showCode">
                  <el-input
                    v-model="loginForm.verifyCode"
                    auto-complete="off"
                    :placeholder="$t('common.verify')"
                    style="width: 63%"
                    @keyup.enter.native="handleLogin"
                  >
                    <svg-icon slot="prefix" icon-class="code" class="el-input__icon input-icon" />
                  </el-input>
                  <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img"/>
                  </div>
                </el-form-item>
              </div>
              <div style="display: flex;justify-content: space-between;margin-bottom:25px">
                <el-checkbox v-model="loginForm.rememberMe">{{ $t('common.rememberMe') }}</el-checkbox>

              </div>
              <div>
                <el-form-item style="width:100%;">
                  <el-button
                    :loading="loading"
                    size="medium"
                    type="primary"
                    style="width:100%;"
                    @click.native.prevent="handleLogin"
                  >
                    <span v-if="!loading">{{ $t('common.login') }}</span>
                    <span v-else>{{ $t('common.loginIng') }}</span>
                  </el-button>
                </el-form-item>
              </div>
              <div style="text-align: right;margin-top: 30px">
                <el-select size="mini" v-model="language" @change="languageChange" style="height:35px">
                  <el-option value="zh" label="中文">中文</el-option>
                  <el-option value="en" label="English">English</el-option>
                </el-select>
                </div>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { tenantInfoList,getCodeImg,property } from "@/api/authority_app/base";
import Cookies from "js-cookie";
import { encrypt,decrypt } from '@/utils/DES3';
import { getRandomSecretKey } from '@/utils/index';

export default {
  name: "Login",
  data() {
    return {
      language: 'zh',
      showMultipleCorporate: false,
      showCode: false,
      //租户列表
      tenantInfoList: [],
      codeUrl: "",
      cookiePasswd: "",
      loginForm: {
        tenantCode: '',
        userName: "",
        inputPwd: '',
        passwd: "",
        rememberMe: false,
        verifyCode: ''
      },
      loginRules: {
        userName: [
          { required: true, trigger: "blur", message: this.$i18n.t('validate.username') }
        ],
        inputPwd: [
          { required: true, trigger: "blur", message: this.$i18n.t('validate.password') }
        ]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getProperty();
    this.getCookie();
  },
  methods: {
    getProperty() {
      property().then((res) => {
        this.showCode = res.result.needVerify;
        this.showMultipleCorporate = res.result.multipleCorporate;
        if(this.showCode) {

          this.getCode();
        }
        this.$store.dispatch("SetCrypto",res.result.crypto)
        this.$store.dispatch("SetSign",res.result.sign)

        this.getTenantInfoList();
      })
    },
    getTenantInfoList() {
      tenantInfoList().then(res => {

        this.tenantInfoList = res.result;
      })
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.result;
      });
    },
    languageChange(value) {
      localStorage.setItem('lang',value)
      window.location.reload()
    },
    getCookie() {
      const userName = Cookies.get("userName");
      const passwd = Cookies.get("passwd");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        userName: userName === undefined ? this.loginForm.userName : userName,
        passwd: passwd === undefined ? this.loginForm.passwd : decrypt(passwd),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
      this.language = localStorage.getItem('lang') || 'zh';
    },
    handleLogin() {
      let that = this;
      this.$refs.loginForm.validate(valid => {
        if (valid) {

          let secretKey = getRandomSecretKey();
          this.loading = true;
          if (this.loginForm.rememberMe) {

            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {

            Cookies.remove('rememberMe');
          }
          // console.log('jm',decrypt('H2bsdDdfEEKpldjubTevcPQf',subForm.inputPwd))
          //如果显示了验证码需要判断下是否填写了
          if(this.showCode && !this.loginForm.verifyCode) {

            this.$message({message: $t('validate.verify'),type: 'warning'})
            return;
          }
          let subForm = {
            tenantCode: encrypt(secretKey,(that.loginForm.tenantCode ? that.loginForm.tenantCode : '')),
            userName: encrypt(secretKey,that.loginForm.userName),
            passwd: encrypt(secretKey,that.loginForm.inputPwd),
            verifyCode: this.loginForm.verifyCode,
            key: secretKey,
          }
          this.$store.dispatch("Login", subForm).then(() => {

            this.$store.dispatch("GetInfo").then((res) => {

              this.$router.push({ path: "/" }).catch(()=>{});
            }).catch(()=>{})
          }).catch((res) => {

            this.loading = false;
            this.getProperty();
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login-page {
  background: url('../assets/images/login_bg.png') center center no-repeat;
  background-size: 100vw 100vh;
  background-position: 0;
  height: 100%;
  display: flex;
  flex-display: column;
  justify-content: center;
  align-items: center;
  .login-body {
    margin: auto;
    display: flex;
    justify-content: space-between;
    background-color: #FFFFFF;
    box-shadow: 0 0 10px 5px rgba(156,171,214,0.08);
    border-radius: 10px;
    padding: 40px 40px;
    .left {
      padding: 0px 20px;
    }
    .page-account-container {
      width: 400px;
      left: 30px;
    }
    .form-body {
      text-align: center;
    }
    .sys-name {
      text-align: center;
      font-size: 24px;
      padding-bottom: 12px;
    }
    .divider {
      border: none;
      background-color: #d8d8d8;
      height: 1px;
      margin-bottom: 31px;
    }
  }
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
